package com.scitc.pleasurevideo.view;


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.adapter.VideoPlayerMenuAdapter;
import com.scitc.pleasurevideo.pojo.PlayMenuTvVo;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

public class VideoPlayerView extends StandardGSYVideoPlayer{

    private ImageView igVideoNextBtn;
    private ImageView igVideoMenuBtn;
    private ImageView igVideoSpeed;
    private ImageView igScreenshot;
    private ListView lvVideoMenu;
    private RelativeLayout rlMoreSeeting;
    private LinearLayout llVideoSpeedBox;
    private VideoPlayerMenuAdapter menuAdapter;
    private TextView tvVideoSpeed5,tvVideoSpeed10,tvVideoSpeed15,tvVideoSpeed20;
    private ImageView lockScreenBtn;
    private List<PlayMenuTvVo> items=new ArrayList<>();
    private Context context;
    public VideoPlayerView(Context context, Boolean fullFlag) {
        super(context, fullFlag);
    }

    public VideoPlayerView(Context context) {
        super(context);
    }

    public VideoPlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public int getLayoutId() {
        return R.layout.view_video_play;
    }

    @Override
    protected void init(Context context) {
        super.init(context);
        this.context=context;
        igVideoNextBtn=(ImageView)findViewById(R.id.igVideoNext);
        igVideoMenuBtn=(ImageView)findViewById(R.id.igVideoMenu);
        igVideoSpeed=(ImageView)findViewById(R.id.igVideoSpeed);
        lockScreenBtn=(ImageView)findViewById(R.id.lock_screen);
        igScreenshot=(ImageView)findViewById(R.id.igScreenshot);
        lvVideoMenu=(ListView) findViewById(R.id.lvVideoMenu);
        rlMoreSeeting=(RelativeLayout) findViewById(R.id.rlMoreSeeting);
        llVideoSpeedBox=(LinearLayout) findViewById(R.id.llVideoSpeedBox);
        tvVideoSpeed5=(TextView)findViewById(R.id.tvVideoSpeed5);
        tvVideoSpeed10=(TextView)findViewById(R.id.tvVideoSpeed10);
        tvVideoSpeed15=(TextView)findViewById(R.id.tvVideoSpeed15);
        tvVideoSpeed20=(TextView)findViewById(R.id.tvVideoSpeed20);

        //点击了选集按钮--展示目录
        igVideoMenuBtn.setOnClickListener(v -> showMoreSeetingItem(lvVideoMenu));
        //点击更多功能面板---将自己隐藏
        rlMoreSeeting.setOnClickListener(v -> closeMoreSettingItem());
        //点击了变速功能--展示变速的选择界面
        igVideoSpeed.setOnClickListener(v -> {
            chengeTvVideoSpeedColor(Double.valueOf(llVideoSpeedBox.getTag().toString()));
            showMoreSeetingItem(llVideoSpeedBox);
        });
        tvVideoSpeed5.setOnClickListener(new VideoSpeedTvClickListener(tvVideoSpeed5));
        tvVideoSpeed10.setOnClickListener(new VideoSpeedTvClickListener(tvVideoSpeed10));
        tvVideoSpeed15.setOnClickListener(new VideoSpeedTvClickListener(tvVideoSpeed15));
        tvVideoSpeed20.setOnClickListener(new VideoSpeedTvClickListener(tvVideoSpeed20));

    }



    /**
     * 视频变速按钮的点击事件
     */
    class VideoSpeedTvClickListener implements OnClickListener {
        TextView itemView;
        VideoSpeedTvClickListener(TextView itemView){
            this.itemView=itemView;
        }
        @Override
        public void onClick(View v) {
            Double speed=Double.valueOf(itemView.getTag().toString());
            chengeVideoSpeed(speed);
            closeMoreSettingItem();
        }
    }

    /**
     * 改变视频播放速度
     * @param speed
     */
    private void chengeVideoSpeed(Double speed){
        llVideoSpeedBox.setTag(speed);
        setSpeedPlaying(speed.floatValue(),false);
        switch (speed+""){
            case "0.5":
                igVideoSpeed.setImageResource(R.mipmap.ic_video_speed_5);
                break;
            case "1.5":
                igVideoSpeed.setImageResource(R.mipmap.ic_video_speed_15);
                break;
            case "2.0":
                igVideoSpeed.setImageResource(R.mipmap.ic_video_speed_20);
                break;
            default:
                igVideoSpeed.setImageResource(R.mipmap.ic_video_speed_10);
                break;
        }
    }
    /**
     * 改变视频播放速度
     * @param speed
     */
    private void chengeTvVideoSpeedColor(Double speed){
        defauleTvVideoSpeedColor();
        switch (speed+""){
            case "0.5":
                tvVideoSpeed5.setTextColor(ContextCompat.getColor(context,R.color.main_color));
                break;
            case "1.5":
                tvVideoSpeed15.setTextColor(ContextCompat.getColor(context,R.color.main_color));
                break;
            case "2.0":
                tvVideoSpeed20.setTextColor(ContextCompat.getColor(context,R.color.main_color));
                break;
            default:
                tvVideoSpeed10.setTextColor(ContextCompat.getColor(context,R.color.main_color));
                break;
        }
    }

    /**
     * 将所有设置视频变速的文字颜色设置为默认黑色
     */
    private void defauleTvVideoSpeedColor(){
        tvVideoSpeed5.setTextColor(ContextCompat.getColor(context,R.color.black));
        tvVideoSpeed15.setTextColor(ContextCompat.getColor(context,R.color.black));
        tvVideoSpeed20.setTextColor(ContextCompat.getColor(context,R.color.black));
        tvVideoSpeed10.setTextColor(ContextCompat.getColor(context,R.color.black));
    }

    /**
     * 设置更多功能面板里面的小功能该显示哪一个
     * @param item
     */
    private void showMoreSeetingItem(View item){
        if(item.getVisibility()==GONE){
            closeMoreSettingItem();
            rlMoreSeeting.setVisibility(VISIBLE);
            item.setVisibility(VISIBLE);
        }else {
            closeMoreSettingItem();
        }
    }

    /**
     * 将更多功能面板里面的小功能全部隐藏
     */
    private void closeMoreSettingItem(){
        rlMoreSeeting.setVisibility(GONE);
        llVideoSpeedBox.setVisibility(GONE);
        lvVideoMenu.setVisibility(GONE);
    }

    /**
     * 设置视频播放速度为默认
     */
    public void setVideoSpeedDefalut(){
        llVideoSpeedBox.setTag(1.0);
        igVideoSpeed.setImageResource(R.mipmap.ic_video_speed_10);
        setSpeedPlaying(1.0F,false);
    }

    /**
     * 获取截屏按钮
     * @return
     */
    public ImageView getIgScreenshot(){
        return igScreenshot;
    }

    /**
     * 获取视频变速按钮
     * @return
     */
    public ImageView getIgVideoSpeed() {
        return igVideoSpeed;
    }

    /**
     * 隐藏跟多功能面板
     */
    public void setRlMoreSeetingGone(){
        closeMoreSettingItem();
    }

    /**
     * 获取下一集按钮
     */
    public ImageView getIgVideoNextBtn() {
        return igVideoNextBtn;
    }

    /**
     * 设置列表菜单按钮的显示或隐藏
     * @param visitible
     */
    public void setIgVideoMenuBtnVisitible(int visitible){
        if(menuAdapter.getCount()<1){
            return;
        }
        igVideoMenuBtn.setVisibility(visitible);
    }

    /**
     * 获取视频列表
     * @return
     */
    public ListView getLvVideoMenu() {
        return lvVideoMenu;
    }

    /**
     * 添加视频播放列表资源
     * @param items
     */
    public void addAllVideoMenu(List<PlayMenuTvVo> items){
        if(menuAdapter==null){
            menuAdapter=new VideoPlayerMenuAdapter(context,this.items);
            lvVideoMenu.setAdapter(menuAdapter);
            menuAdapter.addAll(getItems(items));
        }else {
            menuAdapter.addAll(getItems(items));
        }
        lvVideoMenu.setSelection(menuAdapter.getPosition());
    }

    /**
     * 设置正在播放的集数
     * @param position
     */
    public void setVideoMenuPosition(Integer position){
        menuAdapter.setPosition(position);
        lvVideoMenu.setSelection(position);
    }

    /**
     * 对item 进行 重置 返回
     * @return
     */
    @SuppressWarnings("unchecked")
    private List<PlayMenuTvVo> getItems(List<PlayMenuTvVo> items) {
        return (List<PlayMenuTvVo>) ((ArrayList<PlayMenuTvVo>) items).clone();
    }
}
